using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace BIUtilities.Publish
{
    public class LocalWebFileEdit
    {
        private string _sourceFileUrl;
        private string _targetFileUrl;
        private string _localFileTemp;

        /// <summary>
        /// Source location of Office file. This can be a local or a http:// web location.
        /// </summary>
        public string SourceFile
        {
            get { return _sourceFileUrl; }
            set { _sourceFileUrl = value; }
        }

        /// <summary>
        /// Target location of Office file. This can be a local or a http:// web location.
        /// </summary>
        public string TargetFile
        {
            get { return _targetFileUrl; }
            set { _targetFileUrl = value; }
        }

        /// <summary>
        /// Local temp filename for inherited objects
        /// </summary>
        protected string LocalTempFile
        {
            get { return _localFileTemp; }
            set { _localFileTemp = value; }
        }

        /// <summary>
        /// Load the web file from the target to a staging area for transformations
        /// </summary>
        public void LoadWebFile()
        {
            // Set the local temporary filename
            _localFileTemp = Path.Combine(Environment.CurrentDirectory, Guid.NewGuid().ToString());
            int extensionStart = _sourceFileUrl.LastIndexOf(".");
            if (extensionStart > -1)
            {
                _localFileTemp += _sourceFileUrl.Substring(extensionStart);
            }

            // Load the data
            if (_sourceFileUrl.ToUpper().StartsWith("HTTP"))
            {
                ExecuteWebDownload(_sourceFileUrl, _localFileTemp);
            }
            else
            {
                File.Copy(_sourceFileUrl, _localFileTemp);
            }
        }

        /// <summary>
        /// Save the web file to a target location with the changes made
        /// </summary>
        public void PersistWebFile()
        {
            try
            {
                if (_targetFileUrl.ToUpper().StartsWith("HTTP"))
                {
                    ExecuteWebUpload(_localFileTemp, _targetFileUrl);
                }
                else
                {
                    File.Copy(_localFileTemp, _targetFileUrl);
                }
            }
            finally
            {
                // Remove the file after updating the file
                if (File.Exists(_localFileTemp))
                {
                    File.Delete(_localFileTemp);
                }
            }
        }


        /// <summary>
        /// Execution method for doing updates 
        /// </summary>
        /// <param name="threadContext"></param>
        private void ExecuteWebDownload(string remoteFileUrl, string localFilePath)
        {
            System.Diagnostics.Trace.WriteLine("Web Download: " + remoteFileUrl);
            using (WebClient webClient = new WebClient())
            {
                webClient.UseDefaultCredentials = true;
                webClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                webClient.DownloadFile(remoteFileUrl, localFilePath);
            }
        }

        /// <summary>
        /// Execution method for doing updates 
        /// </summary>
        /// <param name="threadContext"></param>
        private void ExecuteWebUpload(string localFilePath, string remoteFileUrl)
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.UseDefaultCredentials = true;
                webClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                webClient.UploadFile(remoteFileUrl, "PUT", localFilePath);
            }
        }

    }
}
